package xxx;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class DBBean {
	
	private static String url = "";
	private static String driver = "";
	private static String user = "";
	private static String passwd = "";
	private static Connection con = null;
	
	public static void main(String[] args) {
		//System.out.println(DBBean.getConnection());
	}

	static{
		initPath();	
	}
	
	private DBBean(){
		
	}

	private static void initPath(){
	
		String sp = File.separator;
		String path = "/home/flag/ex/myConfigure/db.properties";
		initParam(path);
	}
	
	private static boolean initParam(String paramPath){
		try{
			Properties pro = new Properties();
			pro.load(new FileInputStream(paramPath));
			driver = pro.getProperty("driver");
			url = pro.getProperty("url");
			user = pro.getProperty("user");
			passwd = pro.getProperty("passwd");
			
		}
		catch(Exception e){
			return false;
		}
		return true;
	}
	private static void init(){
		if(con!=null) return;
		try{
			Class.forName(driver);
			con = DriverManager.getConnection(url,user,passwd);
		}
		catch(Exception e){
			System.out.println("获取数据库连接失败！\n"+e);
		}
	}
	
	public synchronized static Connection getConnection(){
		init();
		return con;
	}
	
	public synchronized static void destroy(){
		try{
			con.close();
		}catch(Exception e){
			
		}
	}

}